﻿// ==UserScript==
// @name        AdsRemover
// @namespace   bob
// @version     1
// @require http://code.jquery.com/jquery.js
// ==/UserScript==

/***
console.log("广告清除插件……");
if(top==self){
console.log(location.href);
}else{
console.log("this is a iframe,ignore it => "+location.href);
return;
}
*/
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")(jQuery);";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

function callbackAfterJquery($,window){
function checkAdsIfrm(ifrm) {
		if (arguments.length >= 2) {
			ifrm = arguments[1];
		}
		ifrm = $(ifrm);
		var isAdsIfrm = false;
		//console.log('==============================');
		//console.log(ifrm);
		var src = ifrm.attr("src");
		//console.log('src:' + src);
		//console.log('onload:' + onload);
		var onload = ifrm.attr("onload");
		if (src) {
			if (src.match(/cpro.baidu.com/i)||src.length>200) {
				isAdsIfrm = true;
			}
		}
		if (!isAdsIfrm && onload) {
			if (onload.match(/location.replace/i)||onload.length>200) {
				isAdsIfrm = true;
			}
		}
		if(!isAdsIfrm){
			var name=ifrm.attr("name");
			var id=ifrm.attr("id");
			if((name&&/google.*ads|ads.*google/i.test(name))||(id&&/google.*ads|ads.*google/i.test(id))){
				isAdsIfrm = true;
			}
		}

		return isAdsIfrm;
	}
function handAdObjects(adObjects){
	if(adObjects.size()>0){
		adObjects.attr("isAd", "true");
		console.log("========扫描到广告对象========");
		console.log(adObjects);
		adObjects.hide();	
	}
}
function removeAds() {

	//针对iframe的检测
	var ifms = $("iframe[isAd!='true']").filter(checkAdsIfrm);
	handAdObjects(ifms);
			
	//针对特定网站的检测
	//console.log(location);
	if(location.href.indexOf(".baixing.")>-1){//针对百姓网
		var adObjects = $("div.baiduheadhack[isAd!='true']");
		//console.log(adObjects);
		handAdObjects(adObjects);
	}
	
	//针对搜狐视频播放页面的左右两侧广告
	if(location.href.indexOf("tv.sohu.com")>0){
		var leftAd = $("#video .left");
		//console.log(leftAd);
		leftAd.remove();
		var rightAd = $("#video .right");
		//console.log(rightAd);
		rightAd.remove();
	}
	
	$("div[id^='baidu_clb_slot']").hide();//针对圣城家园
}

var total = 10;//循环5次，检查所有广告，以防动态跳出来的广告
var count = 0;
function proxy(callback){
	removeAds();
	if(typeof callback == 'function'){
		callback();
	}
}

function run(){
	++count;
	//console.log("第"+count+"次运行广告清除插件....");
	var callback = count<total?run:null;
	proxy(function(){
		setTimeout(callback,900);
	});
}

run();
}

if(typeof jQuery=='undefined'){
addJQuery(callbackAfterJquery);
}else{
	callbackAfterJquery(jQuery);
}